<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="30%"
    :before-close="handleClose"
  >
    <div class="dictionary-dialog">
      <el-form
        inline
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="积分活动名称" prop="name" style="width: 100%">
          <el-input
            placeholder="输入积分活动名称"
            style="width: 100%"
            v-model="ruleForm.name"
          />
        </el-form-item>
        <el-form-item label="使用条件" prop="min_money" style="width: 100%">
          满
          <el-input
            placeholder=""
            style="width: 100px; margin: 0 5px"
            v-model="ruleForm.min_money"
          />
          元
        </el-form-item>
        <el-form-item
          label="抵扣比例(%)"
          prop="ratio"
          style="width: 100%; white-space: pre-wrap"
        >
          <el-input
            placeholder=""
            style="width: 100px; margin: 0 5px"
            v-model="ruleForm.ratio"
          />%
          <span style="font-size: 12px; line-height: 20px">
            *最多可以抵扣订单额的比例，如果设置为10%，
            则假如订单额为100元，可抵扣10元。
          </span>
        </el-form-item>
        <el-form-item label="兑换比例" prop="to_money" style="width: 100%">
          <el-input
            placeholder=""
            style="width: 100px; margin: 0 5px"
            v-model="ruleForm.to_money"
          />
          分=1元人民币
        </el-form-item>
        <el-form-item label="用户等级" prop="vip_code" style="width: 100%">
          <el-select
            v-model="ruleForm.vip_code"
            placeholder="用户等级"
            style="width: 100%"
            clearable
            filterable
            multiple
          >
            <el-option
              v-for="item in tableData"
              :key="item.id"
              :label="item.name"
              :value="item.number"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="活动开始时间"
          prop="start_time"
          style="width: 100%"
        >
          <el-date-picker
            style="width: 100%"
            v-model="ruleForm.start_time"
            type="datetime"
            placeholder="活动开始时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="活动结束时间" prop="end_time" style="width: 100%">
          <el-date-picker
            style="width: 100%"
            v-model="ruleForm.end_time"
            type="datetime"
            placeholder="活动结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { addApi, saveApi } from "@/plugins/api/shopping/integral";
import { listApi } from "@/plugins/api/shopping/grade";
let ruleForm = ref({});
let rules = {
  name: [{ required: true, message: "请输入积分活动名称", trigger: "blur" }],
  min_money: [
    { required: true, message: "请输入积分使用条件", trigger: "blur" },
  ],
  ratio: [{ required: true, message: "请输入抵扣比率", trigger: "blur" }],
  to_money: [{ required: true, message: "请输入兑换比例", trigger: "blur" }],
  vip_code: [{ required: true, message: "选择用户等级", trigger: "blur" }],
  start_time: [
    { required: true, message: "选择活动开始时间", trigger: "change" },
  ],
  end_time: [
    { required: true, message: "选择活动结束时间", trigger: "change" },
  ],
};
let dialogVisible = ref(false);
function handleClose(done) {
  // dialogVisible.value = false;
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
}
let title = ref("");
let ruleFormRef = ref(null);
function initFun(type, row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  ruleForm.value = {};
  if (type == "add") {
    title.value = "新增";
  } else {
    title.value = "编辑";
    ruleForm.value = Object.assign({}, row);
  }

  dialogVisible.value = true;
}

const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      let obj = Object.assign({}, ruleForm.value);

      if (!ruleForm.value.id) {
        addApi(obj).then((data) => {
          ElMessage.success("新增成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      } else {
        saveApi(obj).then((data) => {
          ElMessage.success("修改成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      }
    }
  });
}
// 用户等级
let tableData = ref([]);
function getData() {
  listApi({}).then((data) => {
    tableData.value = data.map((item) => {
      item.number = item.number.toString();
      return item;
    });
  });
}
onMounted(() => {
  getData();
});
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss"></style>
